/**
 * 非组合促销活动 - 编辑页面
 * @author miaoyl
 * @date 2019/01/28
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            gwdcz: true,
            form: '',
            disabled: false,
            rules: {
                GWDC009: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                crm_gwcd_gwcd005: app.datas_("crm_gwcd_gwcd005"),
                crm_gwcd_gwcd006: app.datas_("crm_gwcd_gwcd006"),
                crm_gwcd_gwcd007: app.datas_("crm_gwcd_gwcd007")
            }
        },
        cols1 = [[
            {type: 'checkbox', title: '选择'},
            {field: 'GWDC002', width: 80, title: '项序'}
            , {field: 'GWDC016', width: 80, title: '客户编号', event: "open_gwdc016"}
            , {field: 'GWDC017', width: 100, title: '客户名称'}
            , {field: 'GWDC018', width: 140, title: '品号', event: "open_gwdc018"}
            , {field: 'GWAML003', width: 160, title: '品名'}
            , {field: 'GWAML004', width: 80, title: '规格'}
            , {field: 'GWDC019', width: 80, title: '单位说明'}
            , {field: 'GWDC020', width: 80, title: '起订数量', edit: 'text'}
            , {field: 'GWDC021', width: 80, title: '起订金额', edit: 'text'}
            , {field: 'GWDC022', width: 80, title: '阶梯定价'}
            , {field: 'GWDC023', width: 80, title: '结算价'}
            , {field: 'GWDC024', width: 80, title: '活动价格', edit: 'text'}
            , {field: 'GWDC025', width: 140, title: '最大赠品金额', edit: 'text'}
            , {field: 'GWDC026', width: 140, title: '最大赠品数量', edit: 'text'}
        ]],
        cols2 = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWDCZ002', width: 80, title: '项序'}
            , {field: 'GWDCZ003', width: 80, title: '序号'}
            , {field: 'GWDCZ004', width: 140, title: '赠品品号', event: 'open_gwdcz004'}
            , {field: 'GWAML003', width: 160, title: '赠品品名'}
            , {field: 'GWAML004', width: 100, title: '赠品规格'}
            , {field: 'GWDCZ005', width: 80, title: '赠品价格', edit: 'text'}
            , {field: 'GWDCZ006', width: 80, title: '结算价', hide: true}
        ]],
        callObj,
        listData1 = null,
        listData2 = null,
        table_where = {},
        table_select_data = [],
        table,
        gwdc003Table1,
        gwdc003Table2,
        layOpenIndex
    ;

    var Gwdc003Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwdc003_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }]
                }, win);
            },

            initTable1: function () {//商品信息表
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdc003Table1 = table.render({
                        elem: app.getByid(doc, 'gwdc003_edit_table1'),
                        id: 'gwdc003_edit_table1',
                        cols: cols1,
                        height: '250',
                        page: false,
                        // toolbar: '#table-toolbar2',
                        defaultToolbar: ['exports_import'],
                        importsUrl: g_ctxPath + 'crm/gwdc004/importsData',
                        exportsMbUrl: g_ctxPath + 'files/促销价格批量导入.xlsx',
                        where: table_where,
                        done: function (res, curr, count) {
                            //self.reloadUpload('import');
                        }
                    });

                    table.on('tool(gwdc003_edit_table1)', function (obj) {
                        if (obj.event === 'open_gwdc016') {
                            if (vueData.form.GWDC007 == '1') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwae001',
                                            columnList: [{label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                                {label: '说明', prop: 'GWAEL003', width: '150'},
                                                {label: '助记码', prop: 'GWAEL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            obj.update({
                                                'GWDC016': data.GWAE001,
                                                'GWDC017': data.GWAEL003
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择销售渠道信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'cq_gwak001_4',
                                            columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            obj.update({
                                                'GWDC016': data.GWAK001,
                                                'GWDC017': data.GWAKL003
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                        if (obj.event === 'open_gwdc018') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'cq_gwam001_2',
                                        columnList: [{label: '产品编号', prop: 'GWAM001', width: '150'},
                                            {label: '品名', prop: 'GWAML003', width: '150'},
                                            {label: '规格', prop: 'GWAML004', width: '150'},
                                            {label: '单位', prop: 'GWAM008', width: '150'}
                                        ],
                                        dataList: [],
                                        key: 'ROW_ID'
                                    },
                                    cb: function (data) {
                                        var select_data = [];
                                        var select = [];
                                        for (var i = 0; i < data.length; i++) {
                                            select_data.push({
                                                GWDC016: obj.data.GWDC016,
                                                GWDC007: vueData.form.GWDC007,
                                                GWDC018: data[i].GWAM001,
                                                GWDC008: vueData.form.GWDC008
                                            });
                                        }
                                        app.ajax({//获取结算价
                                            xurl: 'crm/gwdc003/getGwdc023',
                                            xdata: select_data,
                                            toJsonStr: true,
                                            cb: function (result) {
                                                if (result.ok) {
                                                    if (result.data.data != "") {
                                                        var data1 = result.data.data;
                                                        var count = obj.data.GWDC002;
                                                        var count2 = parseInt(listData1[listData1.length - 1].GWDC002)+1;
                                                        listData1.removeByMap({
                                                            GWDC002: obj.data.GWDC002
                                                        });
                                                        for (var j = 0; j < data1.length; j++) {
                                                            if (j == 0) {
                                                                count = count;
                                                            } else {
                                                                count = count2;
                                                                count2++;
                                                            }
                                                            listData1.push({
                                                                GWDC002: count,
                                                                GWDC016: obj.data.GWDC016,
                                                                GWDC017: obj.data.GWDC017,
                                                                GWDC018: data1[j].GWAM001,
                                                                GWAML003: data1[j].GWAML003,
                                                                GWAML004: data1[j].GWAML004,
                                                                GWDC019: data1[j].GWAM008,
                                                                GWDC020: '',
                                                                GWDC021: '',
                                                                GWDC022: obj.data.GWDC022,
                                                                GWDC023: data1[j].GWDB016,
                                                                GWDC024: math.mul(data1[j].GWDB016, math.div(vueData.form.GWDC027, 100)).toFixed(2),
                                                                GWDC025: '',
                                                                GWDC026: ''
                                                            });
                                                            select.push(data1[j].GWAM001);
                                                        }
                                                        self.table1Reload();
                                                    }
                                                    var unselect = [];
                                                    // var arr1 = [];
                                                    var arr2 = [];
                                                    /*select.forEach(function (v) {
                                                     arr1.push(v.GWDC018);
                                                     });*/
                                                    select_data.forEach(function (v) {
                                                        arr2.push(v.GWDC018);
                                                    });
                                                    select.concat(arr2).filter(function (item) {
                                                        if (!select.includes(item) || !arr2.includes(item)) {
                                                            unselect.push(item);
                                                        }
                                                    });
                                                    if (result.data.data.length != data.length) {
                                                        var text = '';
                                                        unselect.forEach(function (v) {
                                                            text = text + v + ',';
                                                        });
                                                        text = text.substring(0, text.length - 1);
                                                        app.showMessage('选择的商品' + text + '未设置结算价请检查!');
                                                        return;
                                                    }
                                                }
                                            }
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择产品信息',
                                    area: ['600px', '500px']
                                }
                            });
                        }
                    });

                    //监听行单击事件
                    table.on('row(gwdc003_edit_table1)', function (obj) {
                        table_select_data = obj.data;
                        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                    });

                    table.on('edit(gwdc003_edit_table1)', function (obj) {
                        if (obj.field == 'GWDC021') {
                            var data = obj.data;
                            var GWDC021 = obj.value;
                            if (data.GWDC020 != '' && data.GWDC020 != undefined) {
                                obj.updateOfEdit({
                                    'GWDC021': ''
                                });
                                app.showWarning("起订数量与起订金额只能输一个！");
                                return;
                            }
                            if (GWDC021 > 0) {
                                obj.updateOfEdit({
                                    'GWDC021': GWDC021
                                });
                            } else {
                                obj.updateOfEdit({
                                    'GWDC021': ''
                                });
                                app.showWarning("订单金额必须为正数！");
                                return;
                            }
                        } else if (obj.field == 'GWDC024') {
                            var data = obj.data;
                            var GWDC024 = obj.value;
                            if (GWDC024 > 0 && GWDC024 <= data.GWDC023) {
                                obj.updateOfEdit({
                                    'GWDC024': GWDC024
                                });
                            } else {
                                obj.updateOfEdit({
                                    'GWDC024': math.mul(data.GWDC023, math.div(vueData.form.GWDC027, 100)).toFixed(2)
                                });
                                app.showWarning("活动价必须大于0且不大于结算价！");
                                return;
                            }
                        } else if (obj.field == 'GWDC020') {
                            var data = obj.data;
                            var GWDC020 = obj.value;
                            if (data.GWDC021 != '' && data.GWDC021 != undefined) {
                                obj.updateOfEdit({
                                    'GWDC020': ''
                                });
                                app.showWarning("起订数量与起订金额只能输一个！");
                                return;
                            }
                            var r = /^[0-9]*[1-9][0-9]*$/;
                            var flag = r.test(GWDC020);
                            if (flag) {
                                obj.updateOfEdit({
                                    'GWDC020': GWDC020
                                });
                            } else {
                                obj.updateOfEdit({
                                    'GWDC020': ''
                                });
                                app.showWarning("订单数量必须为正整数！");
                                return;
                            }
                        } else if (obj.field == 'GWDC025') {
                            var data = obj.data;
                            var GWDC025 = obj.value;
                            var r = /^[0-9]*[1-9][0-9]*$/;
                            var flag = r.test(GWDC025);
                            if (GWDC025 == '') {
                                obj.updateOfEdit({
                                    'GWDC025': ''
                                });
                            }
                            if (data.GWDC026 != '' && data.GWDC026 != undefined) {
                                obj.updateOfEdit({
                                    'GWDC025': ''
                                });
                                app.showWarning("最大赠品数量与最大赠品金额只能输一个！");
                                return;
                            }
                            if (GWDC025 != '') {
                                if (flag) {
                                    obj.updateOfEdit({
                                        'GWDC025': GWDC025
                                    });
                                } else {
                                    obj.updateOfEdit({
                                        'GWDC025': ''
                                    });
                                    app.showWarning("最大赠品数量必须为正整数！");
                                    return;
                                }
                            }
                        } else if (obj.field == 'GWDC026') {
                            var data = obj.data;
                            var GWDC026 = $(this).val();
                            if (GWDC026 == '') {
                                obj.updateOfEdit({
                                    'GWDC026': ''
                                });
                            }
                            if (data.GWDC025 != '' && data.GWDC025 != undefined) {
                                obj.updateOfEdit({
                                    'GWDC026': ''
                                });
                                app.showWarning("最大赠品数量与最大赠品金额只能输一个！");
                                return;
                            }
                            if (GWDC026 != '') {
                                if (GWDC026 > 0) {
                                    obj.updateOfEdit({
                                        'GWDC026': GWDC026
                                    });
                                } else {
                                    obj.updateOfEdit({
                                        'GWDC026': ''
                                    });
                                    app.showWarning("最大赠品金额必须大于0！");
                                    return;
                                }
                            }
                        }
                    });

                });
            },

            initTable2: function () {//商品信息表
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdc003Table2 = table.render({
                        elem: app.getByid(doc, 'gwdc003_edit_table2'),
                        id: 'gwdc003_edit_table2',
                        cols: cols2,
                        height: '250',
                        page: false
                    });

                    table.on('tool(gwdc003_edit_table2)', function (obj) {
                        if (obj.event === 'open_gwdcz004') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'cq_gwam001_2',
                                        columnList: [{label: '产品编号', prop: 'GWAM001', width: '150'},
                                            {label: '品名', prop: 'GWAML003', width: '150'},
                                            {label: '规格', prop: 'GWAML004', width: '150'},
                                            {label: '单位', prop: 'GWAM008', width: '150'}
                                        ],
                                        dataList: [],
                                        key: 'ROW_ID'
                                    },
                                    cb: function (data) {
                                        var select_data = [];
                                        for (var i = 0; i < data.length; i++) {
                                            select_data.push({
                                                GWDC016: table_select_data.GWDC016,
                                                GWDC007: vueData.form.GWDC007,
                                                GWDC018: data[i].GWAM001,
                                                GWDC008: vueData.form.GWDC008
                                            });
                                        }
                                        app.ajax({//获取结算价
                                            xurl: 'crm/gwdc003/getZpGwdc023',
                                            xdata: select_data,
                                            toJsonStr: true,
                                            cb: function (result) {
                                                if (result.ok) {
                                                    if (result.data.data != "") {
                                                        var data1 = result.data.data;
                                                        listData2 = gwdc003Table2.getDatas();
                                                        var count = 0;
                                                        if (listData2.length > 0) {
                                                            listData2.forEach(function (v) {
                                                                if (v.GWDCZ002 == table_select_data.GWDC002 && count < parseInt(v.GWDCZ003)) {
                                                                    count = parseInt(v.GWDCZ003);
                                                                }
                                                            });
                                                        }
                                                        count = count + 1;
                                                        //var count = obj.data.GWDCZ003;
                                                        listData2.removeByMap({
                                                            GWDCZ003: obj.data.GWDCZ003,
                                                            GWDCZ002: obj.data.GWDCZ002
                                                        });
                                                        var account = 0;
                                                        for (var j = 0; j < data1.length; j++) {
                                                            if(j == 0){
                                                                account = obj.data.GWDCZ003;
                                                            }else{
                                                                account = count;
                                                                count++;
                                                            }
                                                            listData2.push({
                                                                GWDCZ002: table_select_data.GWDC002,
                                                                GWDCZ003: account,
                                                                GWDCZ004: data1[j].GWAM001,
                                                                GWAML003: data1[j].GWAML003,
                                                                GWAML004: data1[j].GWAML004,
                                                                GWDCZ005: data1[j].GWDB016,
                                                                GWDCZ006: data1[j].GWDB016
                                                            });
                                                        }
                                                        self.table2Reload();
                                                    }
                                                }
                                            }
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择产品信息',
                                    area: ['600px', '500px']
                                }
                            });
                        }
                    });

                    table.on('edit(gwdc003_edit_table2)', function (obj) {
                        if (obj.field == 'GWDCZ005') {
                            var data = obj.data;
                            var GWDCZ005 = obj.value;
                            if (data.GWDCZ006 != '0') {
                                if (GWDCZ005 > 0 && GWDCZ005 <= data.GWDCZ006) {
                                    obj.updateOfEdit({
                                        'GWDCZ005': GWDCZ005
                                    });
                                } else {
                                    obj.updateOfEdit({
                                        'GWDCZ005': data.GWDCZ006
                                    });
                                    app.showWarning("赠品价格必须大于0且不大于结算价！");
                                    return;
                                }
                            } else {
                                if (GWDCZ005 >= 0) {
                                    obj.updateOfEdit({
                                        'GWDCZ005': GWDCZ005
                                    });
                                } else {
                                    obj.updateOfEdit({
                                        'GWDCZ005': '0'
                                    });
                                    app.showWarning("赠品价格必须大于等于0!");
                                    return;
                                }
                            }
                        }
                    });

                });
            },

            table1Reload: function () {
                self._reloadTable({id: 'gwdc003_edit_table1', data: listData1});
            },

            table2Reload: function () {
                table.reload('gwdc003_edit_table2', {
                    data: listData2
                });
            },

            addAllData: function (listData1, listData2) {
                app.ajax({
                    xurl: 'crm/gwdc003/add',
                    xdata: {list1: listData1, list2: listData2, GWDC001: vueData.form.GWDC001},
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc003').reloadTableCurr();
                            self._closeTpl();
                            /*layer.close(layOpenIndex);*/
                        }
                    }
                });
            },

            editAllData: function (listData1, listData2) {
                app.ajax({
                    xurl: 'crm/gwdc003/edit',
                    xdata: {list1: listData1, list2: listData2, GWDC001: vueData.form.GWDC001},
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc003').reloadTableCurr();
                            self._closeTpl();
                            /*layer.close(layOpenIndex);*/
                        }
                    }
                });
            },

            reloadUpload: function (id) {
                layui.use('upload', function () {
                    var upload = layui.upload;
                    var datas = {GWDC007: vueData.form.GWDC007, GWDC008: vueData.form.GWDC008};
                    upload.render({
                        elem: '#' + id,
                        url: 'crm/gwdc004/importsData',
                        accept: 'file',
                        data: datas,
                        acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        done: function (res, index, upload) {
                            if (res.code == 300) {
                                app.showMessage("上传成功");
                                var list = gwdc003Table1.getDatas();
                                var count = list.length + 1;
                                var GWDC022 = 'N';
                                if (vueData.form.GWDC005 != '1') {
                                    GWDC022 = 'Y';
                                }
                                for (var i = 0; i < res.data.length; i++) {
                                    list.push({
                                        'GWDC002': count + i,
                                        'GWDC016': res.data[i].GWDC016,
                                        'GWDC017': res.data[i].GWDC017,
                                        'GWDC018': res.data[i].GWDC018,
                                        'GWAML003': res.data[i].GWAML003,
                                        'GWAML004': res.data[i].GWAML004,
                                        'GWDC019': res.data[i].GWDC019,
                                        'GWDC022': GWDC022,
                                        'GWDC023': res.data[i].GWDC023,
                                        'GWDC024': res.data[i].GWDC024
                                    })
                                }
                                listData1 = list;
                                self.table1Reload();
                            } else {
                                app.showError(res.message);
                            }
                        },
                        error: function (index, upload) {
                            app.showError("上传失败");
                        }
                    });
                });
            },

            _call: function (tagData) {
                /*layOpenIndex = tagData.layOpenIndex;*/
                /*if (tagData.isCheck) {
                 vueData.isCheck = false;
                 } else {
                 vueData.isCheck = true;
                 }*/
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    vueData.disabled = false;
                    vueData.isCheck = false;
                    vueData.gwdcz = true;
                    app.ajax({
                        xurl: 'crm/gwdc003/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                table_where.GWDC007 = '1';
                                table_where.GWDC008 = result.data.data.GWDC008;
                                table_where.GWDC005 = '';
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWDC001: result.data.data.GWDC001,
                                        GWDC008: result.data.data.GWDC008,
                                        GWDC009: result.data.data.GWDC009,
                                        GWDC011: result.data.data.GWDC011,
                                        GWDC011_NAME: result.data.data.GWDC011_NAME,
                                        GWDC012: result.data.data.GWDC012,
                                        GWDC012_NAME: result.data.data.GWDC012_NAME,
                                        GWDC007: '1',
                                        GWDC006: '1',
                                        GWDC004: '2',
                                        GWDC027: 100
                                    },
                                    reset: true
                                });
                                listData1 = [];
                                listData2 = [];
                                self.table1Reload();
                                self.table2Reload();
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    vueData.disabled = true;
                    if (tagData.isCheck) {
                        vueData.isCheck = true;
                        vueData.gwdcz = true;
                    } else {
                        vueData.isCheck = false;
                        vueData.gwdcz = false;
                    }
                    callObj = tagData.obj;
                    table_where.GWDC007 = vueData.form.GWDC007;
                    table_where.GWDC008 = vueData.form.GWDC008;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    /*if(vueData.form.GWDC005 != '3'){
                     gwdc003Table1.config.cols[0][9].edit=false;
                     gwdc003Table1.config.cols[0][8].edit='text';
                     }else{
                     gwdc003Table1.config.cols[0][9].edit='text';
                     gwdc003Table1.config.cols[0][8].edit=false;
                     }*/
                    if (vueData.form.GWDC005 == '3') {
                        gwdc003Table1.config.cols[0][9].edit = 'text';
                        gwdc003Table1.config.cols[0][8].edit = false;
                    } else if (vueData.form.GWDC005 != '1') {
                        gwdc003Table1.config.cols[0][9].edit = false;
                        gwdc003Table1.config.cols[0][8].edit = 'text';
                    }
                    if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                        gwdc003Table1.config.cols[0][13].edit = 'text';
                        gwdc003Table1.config.cols[0][14].edit = 'text';
                        if (!vueData.isCheck) {
                            vueData.gwdcz = false;
                        }
                    } else {
                        gwdc003Table1.config.cols[0][13].edit = false;
                        gwdc003Table1.config.cols[0][14].edit = false;
                        if (!vueData.isCheck) {
                            vueData.gwdcz = true;
                        }
                    }
                    app.ajax({
                        xurl: 'crm/gwdc003/getGwdcData',
                        xdata: {GWDC001: callObj.data.GWDC001},
                        loading: true,
                        cb: function (result) {
                            listData1 = result.data.data;
                            self.table1Reload();
                        }
                    });
                    app.ajax({
                        xurl: 'crm/gwdc003/getGwdczData',
                        xdata: {GWDC001: callObj.data.GWDC001},
                        loading: true,
                        cb: function (result) {
                            listData2 = result.data.data;
                            self.table2Reload();
                        }
                    });
                }

            }

            ,

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['crm_gwdc']);
                var vue = new Vue({
                    el: '#gwdc003_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        addData1: function () {
                            if (vueData.form.GWDC005 == '' || vueData.form.GWDC005 == null) {
                                app.showWarning("请先选择促销方式！");
                                return;
                            }
                            var GWDC022 = 'N';
                            if (vueData.form.GWDC005 != '1') {
                                GWDC022 = 'Y';
                            }
                            listData1 = gwdc003Table1.getDatas();
                            var count = 0;
                            if (listData1.length > 0) {
                                count = parseInt(listData1[listData1.length - 1].GWDC002);
                            }
                            listData1.push({
                                GWDC002: count + 1,
                                GWDC016: '',
                                GWDC017: '',
                                GWDC018: '',
                                GWAML003: '',
                                GWAML004: '',
                                GWDC019: '',
                                GWDC020: '',
                                GWDC021: '',
                                GWDC022: GWDC022,
                                GWDC023: '',
                                GWDC024: '',
                                GWDC025: '',
                                GWDC026: ''
                            });
                            /*if(vueData.form.GWDC005 != '3'){
                             gwdc003Table1.config.cols[0][9].edit=false;
                             gwdc003Table1.config.cols[0][8].edit='text';
                             }else{
                             gwdc003Table1.config.cols[0][9].edit='text';
                             gwdc003Table1.config.cols[0][8].edit=false;
                             }*/
                            if (vueData.form.GWDC005 == '3') {
                                gwdc003Table1.config.cols[0][9].edit = 'text';
                                gwdc003Table1.config.cols[0][8].edit = false;
                            } else if (vueData.form.GWDC005 != '1') {
                                gwdc003Table1.config.cols[0][9].edit = false;
                                gwdc003Table1.config.cols[0][8].edit = 'text';
                            } else if (vueData.form.GWDC005 == '1') {
                                gwdc003Table1.config.cols[0][9].edit = 'text';
                                gwdc003Table1.config.cols[0][8].edit = 'text';
                            }
                            if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                                gwdc003Table1.config.cols[0][13].edit = 'text';
                                gwdc003Table1.config.cols[0][14].edit = 'text';
                            } else {
                                gwdc003Table1.config.cols[0][13].edit = false;
                                gwdc003Table1.config.cols[0][14].edit = false;
                            }
                            self.table1Reload();
                            vueData.disabled = true;
                        },
                        addData2: function () {
                            if (vueData.form.GWDC005 == '1' || vueData.form.GWDC005 == '2') {
                                return;
                            }
                            if (table_select_data == '') {
                                app.showWarning("请先选择商品信息！");
                                return;
                            }
                            if (table_select_data.GWDC016 == '') {
                                app.showWarning("请先维护客户！");
                                return;
                            }
                            listData2 = gwdc003Table2.getDatas();
                            var count = 0;
                            if (listData2.length > 0) {
                                //count = parseInt(listData2[listData2.length-1].GWDCZ003);
                                listData2.forEach(function (v) {
                                    if (v.GWDCZ002 == table_select_data.GWDC002 && count < parseInt(v.GWDCZ003)) {
                                        count = parseInt(v.GWDCZ003);
                                    }
                                });
                            }
                            listData2.push({
                                GWDCZ002: table_select_data.GWDC002,
                                GWDCZ003: count + 1,
                                GWDCZ004: '',
                                GWDCZ005: '',
                                GWDCZ006: ''
                            });
                            self.table2Reload();
                        },
                        cancle: function () {
                            self._closeTpl();
                            /*layer.close(layOpenIndex);*/
                        },
                        delData1: function () {
                            var list = gwdc003Table1.getDatas();
                            var list2 = gwdc003Table2.getDatas();
                            var checkStatus = table.checkStatus('gwdc003_edit_table1');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDC002: v.GWDC002
                                    });
                                    list2.removeByMap({
                                        GWDCZ002: v.GWDC002
                                    })
                                });
                                listData1 = list;
                                listData2 = list2;
                                self.table1Reload();
                                self.table2Reload();
                                if (listData1.length == 0) {
                                    vueData.disabled = false;
                                }
                            }
                        },
                        delData2: function () {
                            var list = gwdc003Table2.getDatas();
                            var checkStatus = table.checkStatus('gwdc003_edit_table2');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDCZ003: v.GWDCZ003,
                                        GWDCZ002: v.GWDCZ002
                                    });
                                });
                                listData2 = list;
                                self.table2Reload();
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    if (vueData.form.GWDC009 > vueData.form.GWDC010) {
                                        app.showError("生效日期不可大于失效日期！");
                                        return;
                                    }
                                    var listData_1 = gwdc003Table1.getDatas();
                                    var checklist = app.cloneJSON(listData_1);
                                    var listData_2 = gwdc003Table2.getDatas();
                                    if (listData_1.length == 0) {
                                        app.showError("商品信息不可为空！");
                                        return;
                                    }
                                    var count_ = 0;
                                    var count_1 = 0;
                                    for (var i = 0; i < listData_1.length; i++) {
                                        var v = listData_1[i];
                                        if (v.GWDC016 == '' || v.GWDC016 == null
                                            || v.GWDC018 == '' || v.GWDC018 == null) {
                                            app.showWarning('请选择客户及商品!');
                                            return;
                                        }
                                        if (vueData.form.GWDC005 == '3') {
                                            if (v.GWDC021 == null || v.GWDC021 == '') {
                                                count_++;
                                            }
                                        } else if (vueData.form.GWDC005 == '1') {
                                            if ((v.GWDC021 == null || v.GWDC021 == '') && (v.GWDC020 == null || v.GWDC020 == '')) {
                                                count_++;
                                            }
                                        } else {
                                            if (v.GWDC020 == null || v.GWDC020 == '') {
                                                count_++;
                                            }
                                        }
                                        if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                                            if ((v.GWDC025 == null || v.GWDC025 == '') && (v.GWDC026 == null || v.GWDC026 == '')) {
                                                count_1++;
                                            }
                                        }
                                    }
                                    if (vueData.form.GWDC005 == '3' && count_ > 0) {
                                        app.showError('起订金额必输!');
                                        return;
                                    }
                                    if (vueData.form.GWDC005 != '3' && vueData.form.GWDC005 != '1' && count_ > 0) {
                                        app.showError('起订数量必输!');
                                        return;
                                    }
                                    if (vueData.form.GWDC005 == '1' && count_ > 0) {
                                        app.showError('起订金额及起订金额任意一个必须有值!');
                                        return;
                                    }
                                    if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                                        if (count_1 > 0) {
                                            app.showError('最大赠送金额及最大赠送量任意一个必须有值!');
                                            return;
                                        }
                                    }
                                    if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                                        if (listData_2.length == 0) {
                                            app.showError("赠品信息不可为空！");
                                            return;
                                        }
                                        for (var i = 0; i < listData_2.length; i++) {
                                            var v = listData_2[i];
                                            if (v.GWDCZ004 == '' || v.GWDCZ004 == null) {
                                                app.showWarning('请选择商品!');
                                                return;
                                            }
                                        }
                                        listData_2.forEach(function (v) {
                                            checklist.removeByMap({
                                                GWDC002: v.GWDCZ002
                                            });
                                        });
                                        if (checklist.length > 0) {
                                            var check_str = "";
                                            for (var i = 0; i < checklist.length; i++) {
                                                if (i == 0) {
                                                    check_str = checklist[i].GWDC002;
                                                } else {
                                                    check_str = check_str + ',' + checklist[i].GWDC002;
                                                }
                                            }
                                            app.showMessage("项序" + check_str + "赠品未维护！");
                                            return;
                                        }
                                    }
                                    listData_1.addMap(vueData.form, false);
                                    /*listData_1.push.apply(listData_1,listData_2);*/
                                    if (vueData.isAdd) {
                                        self.addAllData(listData_1, listData_2);
                                    } else {
                                        self.editAllData(listData_1, listData_2);
                                    }
                                }
                            });
                        },
                        openUser: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [{label: '人员编号', prop: 'ID', width: '150'},
                                            {label: '人员名称', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDC011_NAME = data.NAME;
                                        vueData.form.GWDC011 = data.ID;
                                        vueData.form.GWDC012_NAME = data.ORG_NAME;
                                        vueData.form.GWDC012 = data.ORG_ID;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择申请人',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        gwdc027Change: function (val) {
                            var list = gwdc003Table1.getDatas();
                            vueData.form.GWDC027 = parseFloat(val).toFixed(2);
                            list.forEach(function (v) {
                                v.GWDC024 = math.mul(v.GWDC023, math.div(val, 100)).toFixed(2);
                            });
                            listData1 = list;
                            self.table1Reload();
                        },
                        change7: function () {
                            table_where.GWDC007 = vueData.form.GWDC007;
                        },
                        change5: function (type) {
                            if (type == '1' || type == '2') {
                                vueData.gwdcz = true;
                            } else {
                                vueData.gwdcz = false;
                            }
                            table_where.GWDC005 = vueData.form.GWDC005;
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwdc003_edit');
                    }
                });

                self.initTable1();
                self.initTable2();
            }
            ,
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwdc003Edit();

})
;